<?php

namespace App\Http\Controllers;

use App\Item;
use App\Order;
use App\User;
use Carbon\Carbon;
use Illuminate\Http\Request;
use Cart;
use DB;

class ShopController extends Controller
{
    public function __construct()
    {
        $this->middleware('wechat.oauth', ['only' => ['done']]);
    }

    public $gs = [
        1=>['goods_id'=>1,'goods_name'=>'白百合 清香宜人', 'price'=>23.9],
        2=>['goods_id'=>2,'goods_name'=>'红玫瑰 热烈奔放', 'price'=>23.2],
        3=>['goods_id'=>3,'goods_name'=>'黄牡丹 雍容华贵', 'price'=>23.3],
        4=>['goods_id'=>4,'goods_name'=>'狗尾巴 淡泊名利', 'price'=>23.4],
    ];
    //
    public function index () {
        return view('index' , ['gs'=>$this->gs] );
    }

    public function goods($gid) {
        $g = $this->gs[$gid];
        return view('goods' , $g);
    }

    public function buy($gid) {
        $g = $this->gs[$gid];
        Cart::add($g['goods_id'], $g['goods_name'] ,1, $g['price']);

        return redirect('cart');
    }

    public function cart(){
        if (!Cart::count()) {
            return redirect('/');
        }
        $gs = Cart::content();
        $total = Cart::subtotal();

        return view('cart' , ['gs'=>$gs , 'total'=>$total]);
    }

    public function clear() {
        Cart::destroy();
        return redirect('/');
    }

    /*
  `oid` int(10) unsigned NOT NULL AUTO_INCREMENT,
  `ordsn` char(15) COLLATE utf8_unicode_ci NOT NULL DEFAULT '',
  `uid` int(11) NOT NULL DEFAULT '0',
  `openid` char(32) COLLATE utf8_unicode_ci NOT NULL DEFAULT '',
  `xm` varchar(20) COLLATE utf8_unicode_ci NOT NULL DEFAULT ' ',
  `address` varchar(60) COLLATE utf8_unicode_ci NOT NULL DEFAULT ' ',
  `tel` int(11) NOT NULL DEFAULT '0',
  `money` int(11) NOT NULL DEFAULT '0',
  `ispay` tinyint(4) NOT NULL DEFAULT '0',
  `ordtime` timestamp NULL DEFAULT NULL,
    */
    public function done(Request $request)
    {
        if (!Cart::count()) {
            return '购物车不能是空的';
        }
        $user = $request->session()->get('wechat.oauth');
        $uid = User::where('openid',$user->openid)->value('uid');
        $order = new Order();
        $order->ordsn = date('Ymd').mt_rand('100000','999999');
        $order->uid = $uid;
        $order->openid = $user->openid;
        $order->xm = $request->xm;
        $order->address = $request->address;
        $order->tel = $request->mobile;
        $order->money = Cart::subtotal() * 100;
        $order->ordtime = Carbon::now();
        $order->save();

        //订单对应商品入库
        $items = [];
        $gs = Cart::content();

        foreach ($gs as $g) {
            $items[] = new Item([
                'oid'=>$order->oid,
                'gid' => $g->id,
                'goods_name' => $g->name,
                'num' => $g->qty,
                'price' => $g->price * $g->qty * 100
            ]);
        }

        $order->items()->saveMany($items);

        Cart::destroy(); //下单成功清空购物车

        return view('done', ['oid' => $order->oid, 'price'=>$order->money]);
    }

    public function payok(Request $request)
    {
        $order = Order::find($request->oid);

        if (!$order) {
            return redirect('/');
        }

        $order->ispay = 1;
        $order->save();

        //开始分钱
        $user = User::find($order->uid);
        foreach ([$user->p1=>0.5, $user->p2=>0.3, $user->p3 => 0.1] as $p=>$rate) {
            if ($p == 0) {
                return false;
            }

            $row = [];
            $row['oid'] = $order->oid;
            $row['byid'] = $order->uid;
            $row['uid'] = $order->p;
            $row['money'] = $order->money;

            DB::table('fees')->insert($row);

            return '支付完成';
        }
    }
}
